<template>
   <div id="swiper-img">
     <div class="left" @click="per">
       <span>←</span>
     </div>
     <div v-for="(item,index) in images" v-show="n == index"  @mouseout="stop" @mouseover="start" class="item">
       <a :href="item.link">
         <img :src="item.image" alt="item" >
       </a>
     </div>
     <div v-for="(item,index) in images" v-show="n == index"  @mouseout="stop" @mouseover="start" class="item">
     </div>
     <div class="page">
      <ul>
         <li  v-for="(item,index) in images" :class="n == index ? 'active' : '' " :key="index"></li>
      </ul>
     </div>

     <div class="right" @click="next">
       <span> →</span>
     </div>
   </div>
</template>

<script>
export default {
  name: "SwiperItem",
  data () {
    return {
      n:0,
      interId:null
    }
  },
  props:{
    images:{
      type:Array,
      default () {
        return ['https://s10.mogucdn.com/mlcdn/c45406/180926_45fkj8ifdj4l824l42dgf9hd0h495_750x390.jpg',
        'https://s10.mogucdn.com/mlcdn/c45406/180919_47iclhel8f4ld06hid21he98i93fc_750x390.jpg',
      ] }
    }
  },


  methods:{
    go () {
    this.interId = setInterval( n =>{
        if (this.n<this.images.length-1){
          this.n++
        }else {
          this.n=0;
        }
      // console.log(this.images);
    },3000 )
    },
    per () {
      clearInterval(this.interId);
      if (this.n <= 0){
        this.n=this.images.length-1;
      }else this.n--;
     this.go()
    },
    next () {
      clearInterval(this.interId);
      if (this.n>=this.images.length-1){
        this.n=0;
      }else this.n++
     this.go()
    },
    stop (){
      clearInterval(this.interId);
    },
    start () {
      this.go()
    }
  },
  mounted() {
    this.go()
  }
}
</script>

<style scoped>

img{
width: 100%;
}

#swiper-img{

  position: relative;
}
span{
  position: absolute;
  top:18px;
  left: 0;
  color: black;
}
.right {
  display: flex;
  font-size: 30px;
  position: absolute;
  right: 0;
  bottom: 0;
  width: 30px;
  height: 75px;
  background-color:ghostwhite;
  opacity: 0.3;
  border: lavenderblush 1px solid;
}
.left {
  z-index: 1;
  font-size: 30px;
  position: absolute;
  display: block;
  left: 0;
  bottom: 0;
  width: 30px;
  height: 75px;
  background-color:ghostwhite;
  opacity: 0.3;
  border: lavenderblush 1px solid;
}
.page{
  -moz-box-align: center;
  display: flex;
  position: absolute;
  bottom: 15px;
  left: 130px;
}
ul{
  display: flex;
  text-align: center;
  opacity: 1;
}
li{
width: 15px;
  flex:1;
}
.active{
  color: white;
}
</style>